package cn.edu.uestc.smgt.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.uestc.smgt.common.Response;
import cn.edu.uestc.smgt.common.Status;
import cn.edu.uestc.smgt.dao.UserMapper;
import cn.edu.uestc.smgt.pojo.User;
import cn.edu.uestc.smgt.service.UserService;

@Controller
@RequestMapping("/usr/")
public class LoginController {
	@Autowired
	private UserService userService;

	@Autowired
	private UserMapper userMapper;

	/**
	 * 管理员或用户登录
	 * 
	 * @param map
	 * @param session
	 * @return
	 */
	@RequestMapping("login")
	@ResponseBody
	public Object login(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			@RequestParam HashMap map) {
		String username = (String) map.get("username");
		String password = (String) map.get("password");
		String role = (String) map.get("role");
		int status = userService.checkUser(username, password, role);
		if (status == Status.ROLE_NOT_MATCH) {
			return new Response(status, "角色不匹配");
		}
		if (status == Status.USER_IS_NULL)
			return new Response(status, "用户名或密码错误!");
		return new Response(status);
	}

	/**
	 * 管理员或用户登录
	 * 
	 * @param map
	 * @param session
	 * @return
	 */
	@RequestMapping("login1")
	@ResponseBody
	public Object login1(@RequestParam HashMap map, HttpSession session) {
		String username = (String) map.get("username");
		String password = (String) map.get("password");
		String role = (String) map.get("role");
		User user = new User();
		user.setLoginName(username);
		user.setPwd(password);
		User getUser = userMapper.selectByLogin(user);
		if (getUser == null)
			return new Response(Status.USER_IS_NULL, "用户名或密码错误!");
		Byte role1 = Byte.parseByte(role);
		if (getUser.getRole() != role1)
			return new Response(Status.ROLE_NOT_MATCH, "角色不匹配");
		session.setAttribute("user", getUser);
		return new Response(Status.SUCCESS);
	}

	/**
	 * 管理员登录状态判断
	 * 
	 * @param session
	 * @return
	 */
	/*
	 * @RequestMapping("mgr/loginstatus")
	 * 
	 * @ResponseBody public HashMap getManagerLoginStatu(HttpSession session) {
	 * HashMap result = new HashMap(); Object mgr =
	 * session.getAttribute("manager"); if (mgr == null) { result.put("status",
	 * 0); result.put("message", "请登录"); } else { result.put("status", 1);
	 * result.put("message", "已登录"); } return result; }
	 *//**
	 * 获取管理员信息。
	 * 
	 * @param session
	 * @return
	 */
	/*
	 * @RequestMapping("mgr/mgrinfo")
	 * 
	 * @ResponseBody public HashMap getManagerInfo(HttpSession session) {
	 * HashMap result = new HashMap(); Object mgr =
	 * session.getAttribute("manager"); if (mgr == null) { result.put("status",
	 * 0); result.put("message", "请登录"); } else { result.put("status", 1);
	 * result.put("username", ((Manager) mgr).getUsername());
	 * result.put("message", "已登录"); } return result; }
	 *//**
	 * 修改管理员密码
	 */
	/*
	 * @RequestMapping("mgr/changepwd")
	 * 
	 * @ResponseBody public HashMap changeMgrPwd(@RequestParam HashMap map,
	 * HttpSession session) { HashMap result = new HashMap(); Object mgr =
	 * session.getAttribute("manager"); if (mgr == null) { result.put("status",
	 * 0); result.put("message", "请登录"); } else { String username = ((Manager)
	 * mgr).getUsername(); String password = (String) map.get("password");
	 * String newPwd = (String) map.get("newPwd"); if
	 * (managerService.managerChangePwd(username, password, newPwd)) {
	 * result.put("status", 1); result.put("message", "修改成功"); } else {
	 * result.put("status", 0); result.put("message", "修改失败"); } } return
	 * result; }
	 *//**
	 * 二级用户登录
	 * 
	 * @param map
	 * @param session
	 * @return
	 */
	/*
	 * @RequestMapping("yh")
	 * 
	 * @ResponseBody public HashMap yhLogin(@RequestParam HashMap map,
	 * HttpSession session) { String username = (String) map.get("username");
	 * String password = (String) map.get("password"); HashMap result = new
	 * HashMap(); String message = null; int status = 0; if (username == null ||
	 * "".equals(username)) { message = "账号不能为空"; status = 0; } else { User user
	 * = userService.yhLogin(username, password); if (user == null) { message =
	 * "账号或密码错误"; status = 0; } else { session.setAttribute("user", user);
	 * message = "登录成功"; status = 1; } } result.put("message", message);
	 * result.put("status", status); return result; }
	 *//**
	 * 获取二级用户信息。
	 * 
	 * @param session
	 * @return
	 */
	/*
	 * @RequestMapping("yh/yhinfo")
	 * 
	 * @ResponseBody public HashMap getYhInfo(HttpSession session) { HashMap
	 * result = new HashMap(); User yh = (User) session.getAttribute("user"); if
	 * (yh == null) { result.put("status", 0); result.put("message", "请登录"); }
	 * else { String unitId = yh.getUnitId(); // 查询对应的单位名称 Unit u =
	 * unitService.getUnitById(unitId); result.put("status", 1);
	 * result.put("username", ((User) yh).getUsername()); result.put("unitName",
	 * u.getName()); result.put("message", "已登录"); } return result; }
	 *//**
	 * 修改二级用户密码
	 */
	/*
	 * @RequestMapping("yh/changepwd")
	 * 
	 * @ResponseBody public HashMap changeYhPwd(@RequestParam HashMap map,
	 * HttpSession session) { HashMap result = new HashMap(); Object yh =
	 * session.getAttribute("user"); if (yh == null) { result.put("status", 0);
	 * result.put("message", "请登录"); } else { String username = ((User)
	 * yh).getUsername(); String password = (String) map.get("password"); String
	 * newPwd = (String) map.get("newPwd"); if
	 * (userService.yhChangePwd(username, password, newPwd)) {
	 * result.put("status", 1); result.put("message", "修改成功"); } else {
	 * result.put("status", 0); result.put("message", "修改失败"); } } return
	 * result; }
	 */

}
